package PacoteFinal;

import au.com.bytecode.opencsv.CSVWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Vitor
 */

public class Janela extends javax.swing.JFrame {
    
    private listaFilmes lista = new listaFilmes();
    private ArrayList<Integer> listaS = new ArrayList<Integer>();
    
    private Timer timer;
    
      
    public Janela()
    {  
        init_components();
        
        // Banner
        
        Banner = new Banner(this.getBounds());
        
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask()
        {
            @Override
            public void run()
            {
                Banner.run();
            }
        }, 0, 2*1000);
        
        this.add(Banner.Label);
    }
    
    private void init_components()
    {
        // Banner
        this.setLayout(null);
        this.setSize(800, 800);
        
        
        
        // Default
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        
        // JButtons
            // PanesFilmes -- Button1
            {
        
            PaneFilmes = new javax.swing.JFrame();
            PaneFilmes.setLayout(null);
            
            javax.swing.JButton Continue = new javax.swing.JButton("Continue");
            javax.swing.JButton Cancel = new javax.swing.JButton("Cancel");
            
            
            PaneFilmes.setBounds(this.getWidth()/4, this.getHeight()/4, this.getWidth()/2, this.getHeight()/2);
            
            Continue.setBounds(PaneFilmes.getWidth()/4, PaneFilmes.getHeight()-150, 100, 40);
            Cancel.setBounds(2*(PaneFilmes.getWidth()/4), PaneFilmes.getHeight()-150, 100, 40);
            
            javax.swing.JLabel label1 = new javax.swing.JLabel("Nome: ");
            javax.swing.JLabel label2 = new javax.swing.JLabel("Data (dd/mm/aa): ");
            javax.swing.JLabel label3 = new javax.swing.JLabel("Horario (hh:mm): ");
            javax.swing.JLabel label4 = new javax.swing.JLabel("Ingresso: ");
            javax.swing.JLabel label5 = new javax.swing.JLabel("Sala: ");
            
            label1.setBounds(5, 25, 100, 20);
            label2.setBounds(5, 50, 100, 20);
            label3.setBounds(5, 75, 100, 20);
            label4.setBounds(5, 100, 100, 20);
            label5.setBounds(5, 125, 100, 20);
                    
                       
            text1.setBounds(100, 25, PaneFilmes.getWidth()-120, 20);
            text2.setBounds(100, 50, PaneFilmes.getWidth()-120, 20);
            text3.setBounds(100, 75, PaneFilmes.getWidth()-120, 20);
            text4.setBounds(100, 100, PaneFilmes.getWidth()-120, 20);
            cb5.setBounds(100, 125, PaneFilmes.getWidth()-120, 20);
            
            PaneFilmes.add(label1);
            PaneFilmes.add(label2);
            PaneFilmes.add(label3);
            PaneFilmes.add(label4);
            PaneFilmes.add(label5);
            
            PaneFilmes.add(text1);
            PaneFilmes.add(text2);
            PaneFilmes.add(text3);
            PaneFilmes.add(text4);
            PaneFilmes.add(cb5);
            
            PaneFilmes.add(Continue);
            PaneFilmes.add(Cancel);
            
            
            Continue.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    try
                    {
                        buttonContinueActionPerformed(evt, text1.getText(), text2.getText(), text3.getText(), Integer.parseInt(text4.getText()), Integer.parseInt(cb5.getSelectedItem().toString()));
                    }
                    catch(NumberFormatException e)
                    {
                        erro_continue();
                    }
                    catch(NullPointerException e)
                    {
                        erro_sala();
                    }
                   
                }
            });
            
            Cancel.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonCancelActionPerformed(evt);
                }
            });
         
            }
            
            // Button1
            button1 = new javax.swing.JButton("Inserir filmes");
            button1.setBounds(this.getWidth()/2+30, this.getHeight()/3 + 50, this.getWidth()/6-5, this.getHeight()/32);
            
            button1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonInserirFilmesActionPerformed(evt);
                }
            });
        
        
        // Button2
        button2 = new javax.swing.JButton("Vender Ingresso");
        button2.setBounds(this.getWidth()/2+this.getWidth()/6+60, this.getHeight()/3 + 50, this.getWidth()/6-5, this.getHeight()/32);
        
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVenderActionPerformed(evt);
            }
        });
        
        // Button3
        button3 = new javax.swing.JButton("Remove Entrada");
        button3.setBounds(this.getWidth()/2+30, this.getHeight()/3+70+this.getHeight()/32, this.getWidth()/6-5, this.getHeight()/32);
        
        button3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRemoveEntradaActionPerformed(evt);
            }
        });
        
        // Button4
        button4 = new javax.swing.JButton("Modificar Salas");
        button4.setBounds(this.getWidth()/2+this.getWidth()/6+60, this.getHeight()/3+70+this.getHeight()/32, this.getWidth()/6-5, this.getHeight()/32);
        
        button4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonModificarSalaActionPerformed(evt, listaS.size());
            }
        });
        
        
        // Button5
        button5 = new javax.swing.JButton("Salvar relatorio");
        button5.setBounds(20, this.getHeight()-100, this.getWidth()/3, this.getHeight()/25);
        
        button5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    buttonSalvarActionPerformed(evt);
                } catch (IOException ex) {
                    Logger.getLogger(Janela.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
            
        
        // JTable
        table = new javax.swing.JTable();
        table.setModel(lista);

        
        
        // ScrollPane
        scroll = new javax.swing.JScrollPane();
        scroll.setBounds(20, this.getHeight()/3 + 40, this.getWidth()/2-20, this.getHeight()/3);
        scroll.setViewportView(table);
        
        
        // Adds
        this.add(scroll);
        this.add(button1);
        this.add(button2);
        this.add(button3);
        this.add(button4);
        this.add(button5);
    }
    
    public static void main(String[] args)
    {      
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Janela().setVisible(true);
            }
        });
    }
    
    private void erro_continue()
    {
        javax.swing.JOptionPane erro = new javax.swing.JOptionPane();
        erro.showMessageDialog(this, "Há campos inválidos", "Erro venda", erro.ERROR_MESSAGE);
    }
    
    private void erro_sala()
    {
        javax.swing.JOptionPane erro = new javax.swing.JOptionPane();
        erro.showMessageDialog(this, "Sala incorreta", "Erro sala", erro.ERROR_MESSAGE);
    }
    
    private void buttonSalvarActionPerformed(java.awt.event.ActionEvent evt) throws IOException
    {
        String csv = "C:/Relatorio.csv";
        
        javax.swing.JOptionPane erro = new javax.swing.JOptionPane();
        erro.showMessageDialog(this, "O Arquivo foi salvo em: " + csv, "Relatorio", erro.PLAIN_MESSAGE);
        
        CSVWriter writer = new CSVWriter(new FileWriter(csv));
        
        List<String[]> data = new ArrayList<String[]>();
        
        
        for(int i = 0; i < lista.getRowCount(); i++)
        {
            data.add(new String[] {"START POINT:\n","Nome do Filme: ", lista.getValueAt(i, 0).toString() + "\n", "Secao: ", ((Integer)(i+1)).toString(), "\n", "Ingressos vendidos: ", ((Integer)(lista.getIngressosOriginal(i)-lista.getIngressos(i))).toString(), ".\n\n", "END OF FILE\n"});         
        }
        
        
        writer.writeAll(data);        
 
        writer.close();
    }
    
   
    private void buttonInserirFilmesActionPerformed(java.awt.event.ActionEvent evt) 
    {                                              
        // TODO add your handling code here:
        
        PaneFilmes.setVisible(true);
    }
    
    private void buttonContinueActionPerformed(java.awt.event.ActionEvent evt, String name, String data, String horario, int ingressos, int sala)
    {
        lista.inserir(name, data, horario, ingressos, sala);
        PaneFilmes.setVisible(false);
            
        text1.setText(null);
        text2.setText(null);
        text3.setText(null);
        text4.setText(null);
    }
    
    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt)
    {
        PaneFilmes.setVisible(false);
        
        text1.setText(null);
        text2.setText(null);
        text3.setText(null);
        text4.setText(null);
    }
    
    private void buttonVenderActionPerformed(java.awt.event.ActionEvent evt)
    {
        javax.swing.JOptionPane opt = new javax.swing.JOptionPane();
        
        String s = (String)opt.showInputDialog(
                    this,
                    "Selecione o nome do filme\n"
                    + "Que desejas vender ingresso:",
                    "Venda de Ingresso",
                    opt.PLAIN_MESSAGE,
                    null,
                    lista.getFilme(),
                    "");
        
        if ( s != null )
        {
            if(lista.setIngressos(s))
            {
                javax.swing.JOptionPane erro = new javax.swing.JOptionPane();
                erro.showMessageDialog(this, "Não tem mais ingressos disponíveis para venda", "Erro venda", erro.ERROR_MESSAGE);
            }
        }
            
    }
    
    private void buttonRemoveEntradaActionPerformed(java.awt.event.ActionEvent evt) 
    {                                              
        int index = table.getSelectedRow();
        if (index == -1)
            return;
        lista.remove(index);
    } 
    
    private void buttonModificarSalaActionPerformed(java.awt.event.ActionEvent evt, Integer nr)
    {
        javax.swing.JOptionPane opt = new javax.swing.JOptionPane();
        
        Object[] options = {"Adicionar 1",
                            "Cancelar",
                            "Remove 1"};
                
        int s = opt.showOptionDialog(
                    this,
                    "Já possuimos: " + nr.toString()
                    + "\nO Que deseja fazer?",
                    "Modificar Salas",
                    opt.YES_NO_CANCEL_OPTION,
                    opt.QUESTION_MESSAGE,
                    null,
                    options,
                    options[2]);
        
        
        if( s == 0)
        {
            listaS.add(listaS.size()+1);
            cb5.addItem(listaS.size());
        }
        if( s == 2 )
        {
            try
            {
                cb5.removeItem(listaS.size());
                listaS.remove(listaS.size()-1);
            }
            catch(NullPointerException e)
            {
                javax.swing.JOptionPane erro = new javax.swing.JOptionPane();
                erro.showMessageDialog(this, "Já tens 0 salas", "Erro salas", erro.ERROR_MESSAGE);
            }
                
        }
    }
    // VARIÁVEIS
    
    private javax.swing.JTextField text1 = new javax.swing.JTextField(1);
    private javax.swing.JTextField text2 = new javax.swing.JTextField(1);
    private javax.swing.JTextField text3 = new javax.swing.JTextField(1);
    private javax.swing.JTextField text4 = new javax.swing.JTextField(1);
    private javax.swing.JComboBox cb5 = new javax.swing.JComboBox(listaS.toArray());
    
    private javax.swing.JTable table;
    
    private javax.swing.JScrollPane scroll;
    
    private javax.swing.JButton button1;
    private javax.swing.JButton button2;
    private javax.swing.JButton button3;
    private javax.swing.JButton button4;
    private javax.swing.JButton button5;
    private javax.swing.JButton button6;
    
    private Banner Banner;
    
    private javax.swing.JFrame PaneFilmes;
    
    // END OF VARIÁVEIS
    
}

